package com.ziyun.pop.web.modules.quoteFactoryMaterial.controller;

import com.utility.JsonUtils;
import com.utility.http.HttpUtils;
import com.ziyun.pop.common.utils.UserUtils;
import com.ziyun.pop.modules.quoteFactoryMaterial.entity.ApiMaterialEntity;
import com.ziyun.pop.modules.quoteFactoryMaterial.entity.QuoteAttributeOptionEntity;
import com.ziyun.pop.modules.quoteFactoryMaterial.entity.QuoteFactoryMaterialEntity;
import com.ziyun.pop.modules.quoteFactoryMaterial.entity.QuoteFactoryMaterialVO;
import io.renren.common.annotation.SysLog;
import io.renren.common.utils.R;
import io.renren.modules.sys.entity.SysUserEntity;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;


/**
 * 工厂材料价格表
 *
 * @author jl
 * @email
 * @date 2018-09-12 09:04:01
 */
@RestController
@RequestMapping("quoteFactoryMaterial/quotefactorymaterial")
public class QuoteFactoryMaterialController {

    @Value("${erpApi}")
    private String erpApi;

    /**
     * 列表
     */
    @RequestMapping("/list")
    @RequiresPermissions("quoteFactoryMaterial:quotefactorymaterial:list")
    public R list(@RequestParam Map<String, Object> params) {
        // 获取当前用户的商户id
        Long sellerId = UserUtils.getCurentUser().getSellerId();
        Map<String, Object> paramBodyMap = new HashMap<String, Object>();
        //pop_sys_user表seller_id是工厂id
        params.put("factoryId", sellerId);
        String url = erpApi + "/quoteFactoryMaterial/quotefactorymaterial/list";
        String response = HttpUtils.doPost(url, params);
        R rs = JsonUtils.string2Object(response, R.class);

        return rs;
    }


    /**
     * 信息
     */
    @RequestMapping("/info/{id}")
    @RequiresPermissions("quoteFactoryMaterial:quotefactorymaterial:info")
    public R info(@PathVariable("id") Integer id) {
        Map<String, Object> paramBodyMap = new HashMap<String, Object>();
        paramBodyMap.put("id", id);
        paramBodyMap.put("factoryId",UserUtils.getCurentUser().getSellerId());
        String url = erpApi + "/quoteFactoryMaterial/quotefactorymaterial/info";
        String response = HttpUtils.doPost(url, paramBodyMap);
        R rs = JsonUtils.string2Object(response, R.class);

        return rs;
    }

    /**
     * 保存
     */
    @RequestMapping("/save")
    @RequiresPermissions("quoteFactoryMaterial:quotefactorymaterial:save")
    public R save(@RequestBody List<QuoteFactoryMaterialEntity> quoteFactoryMaterialList) {
        SysUserEntity curentUser = UserUtils.getCurentUser();
        ApiMaterialEntity apiMaterialEntity = new ApiMaterialEntity(quoteFactoryMaterialList,UserUtils.getCurentUser().getSellerId(),curentUser.getUsername());
        String url = erpApi + "/quoteFactoryMaterial/quotefactorymaterial/save";
        String response = HttpUtils.doPost(url, apiMaterialEntity);
        R rs = JsonUtils.string2Object(response, R.class);

        return rs;
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
    @RequiresPermissions("quoteFactoryMaterial:quotefactorymaterial:update")
    public R update(@RequestBody List<QuoteFactoryMaterialEntity> quoteFactoryMaterialList) {
        SysUserEntity curentUser = UserUtils.getCurentUser();
        ApiMaterialEntity apiMaterialEntity = new ApiMaterialEntity(quoteFactoryMaterialList,UserUtils.getCurentUser().getSellerId(),curentUser.getUsername());
        String url = erpApi + "/quoteFactoryMaterial/quotefactorymaterial/update";
        String response = HttpUtils.doPost(url, apiMaterialEntity);
        R rs = JsonUtils.string2Object(response, R.class);

        return rs;
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
    @RequiresPermissions("quoteFactoryMaterial:quotefactorymaterial:delete")
    public R delete(@RequestBody Integer[] ids) {

        Map<String, Object> paramBodyMap = new HashMap<String, Object>();
        paramBodyMap.put("ids", ids);
        paramBodyMap.put("factoryId",UserUtils.getCurentUser().getSellerId());
        String url = erpApi + "/quoteFactoryMaterial/quotefactorymaterial/update";
        String response = HttpUtils.doPost(url, paramBodyMap);
        R rs = JsonUtils.string2Object(response, R.class);

        return rs;
    }

    /**
     * 增加克重弹窗 根据工厂id和选项id联合查询某工厂的材料克重
     */
    @GetMapping("addWeight/{optionKey}")
    public List<QuoteFactoryMaterialEntity> addWeight(@PathVariable String optionKey) {
        // 获取当前用户的商户id
        Long sellerId = UserUtils.getCurentUser().getSellerId();
        Map<String, Object> paramBodyMap = new HashMap<String, Object>();
        //pop_sys_user表seller_id是工厂id
        paramBodyMap.put("factoryId", sellerId);
        paramBodyMap.put("optionKey", optionKey);

        String url = erpApi + "/quoteFactoryMaterial/quotefactorymaterial/addWeight";
        String response = HttpUtils.doPost(url, paramBodyMap);
        List<QuoteFactoryMaterialEntity> rs = JsonUtils.string2Object(response, List.class);

        return rs;
    }

    /**
     * 启用/禁用
     */
    @PostMapping("/setStart/{id}/{value}")
    public R setStart(@PathVariable Integer id, @PathVariable Boolean value) {
        Map<String, Object> paramBodyMap = new HashMap<String, Object>();
        paramBodyMap.put("id", id);
        paramBodyMap.put("value", value);
        paramBodyMap.put("factoryId",UserUtils.getCurentUser().getSellerId());
        String url = erpApi + "/quoteFactoryMaterial/quotefactorymaterial/setStart";
        String response = HttpUtils.doPost(url, paramBodyMap);
        R rs = JsonUtils.string2Object(response, R.class);

        return rs;
    }

    //页面leftTable数据
    @RequestMapping("/getLeftTableData")
    public  List<QuoteFactoryMaterialVO> getLeftTableData() {
        Long sellerId = UserUtils.getCurentUser().getSellerId();
        Map<String, Object> paramBodyMap = new HashMap<String, Object>();
        //pop_sys_user表seller_id是工厂id
        paramBodyMap.put("factoryId", sellerId);
        String url = erpApi + "/quoteFactoryMaterial/quotefactorymaterial/getLeftTableData";
        String response = HttpUtils.doPost(url, paramBodyMap);
        List<QuoteFactoryMaterialVO> list = JsonUtils.string2Collection(response, List.class, QuoteFactoryMaterialVO.class);
        return list;
    }

    //删除
    @RequestMapping("/delWeight")
    public R delWeight(String optionName) {
        Long sellerId = UserUtils.getCurentUser().getSellerId();
        Map<String, Object> paramBodyMap = new HashMap<String, Object>();
        //pop_sys_user表seller_id是工厂id
        paramBodyMap.put("factoryId", sellerId);
        paramBodyMap.put("optionName", optionName);
        String url = erpApi + "/quoteFactoryMaterial/quotefactorymaterial/delWeight";
        String response = HttpUtils.doPost(url, paramBodyMap);
        R rs = JsonUtils.string2Object(response, R.class);
        return rs;
    }

    /**
     * 得到材料选项集合
     */
    @GetMapping("/getMaterialList")
    public List<QuoteAttributeOptionEntity> getMaterialList() {
        String url = erpApi + "/quoteFactoryMaterial/quotefactorymaterial/getMaterialList";
        String response = HttpUtils.doPost(url, null);
        List<QuoteAttributeOptionEntity> list = JsonUtils.string2Collection(response,List.class, QuoteAttributeOptionEntity.class);
        return list;
    }

    //设为特种纸
    @RequestMapping("/specialPaper")
    public R specialPaper(@RequestBody Integer[] ids) {
        String url = erpApi + "/quoteFactoryMaterial/quotefactorymaterial/specialPaper";
        String response = HttpUtils.doPost(url, ids);

        return R.ok();
    }

    //取消特种纸
    @RequestMapping("/unSpecialPaper")
    public R unSpecialPaper(@RequestBody Integer[] ids) {
        String url = erpApi + "/quoteFactoryMaterial/quotefactorymaterial/unSpecialPaper";
        String response = HttpUtils.doPost(url, ids);

        return R.ok();
    }
}
